package com.jgp.security.shiro;

import org.apache.shiro.aop.AnnotationResolver;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;

/**
 * 项目   parent
 * 作者   loufei
 * 时间   2018/4/13
 */
public class AdminPermissionAnnotationMethodInterceptor  extends AuthorizingAnnotationMethodInterceptor {

    public AdminPermissionAnnotationMethodInterceptor() {
        super( new AdminPermissionAnnotationHandler() );
    }

    public AdminPermissionAnnotationMethodInterceptor(AnnotationResolver resolver) {
        super( new AdminPermissionAnnotationHandler(), resolver);
    }

    @Override
    public void assertAuthorized(org.apache.shiro.aop.MethodInvocation mi) throws AuthorizationException {
        try {
            ((AdminPermissionAnnotationHandler)getHandler()).assertAuthorized(getAnnotation(mi),mi.getMethod());
        }
        catch(AuthorizationException ae) {
            // Annotation handler doesn't know why it was called, so add the information here if possible.
            // Don't wrap the handler here since we don't want to mask the specific handler, such as
            // UnauthenticatedException etc.
            if (ae.getCause() == null) ae.initCause(new UnauthorizedException("Not authorized to invoke method: " + mi.getMethod()));
            throw ae;
        }
    }
}